'use strict';

var Router = require('koa-router');
var config = require('../../../config/index.js');
var dao = require('../../../dao/face_part.js');
var daoStar = require('../../../dao/face_star.js');
var appUtil = require('../../../common/app_utils.js');
var errCodes = require('../../../common/err_code.js');
//
var __pathPrefix = config.baseUrl + '/v1/face/parts';
appUtil.appLog('router:' + __pathPrefix);
var router = new Router({prefix: __pathPrefix});
module.exports = router.routes();

// 部位编号
var EnumParts = config.EnumParts;

/**
 * 获取所有 面部组建
 * get /api/v1/face/parts/all
 */
router.get('/all', function*() {
  // 参数判断
  var allItem = yield dao.getAll();
  var allStar;
  if (this.request.inParams.fields.manager) {
    var stars = yield daoStar.getAll();
    allStar = {};
    stars.forEach(function (item) {
      var id = item.starid;
      allStar[id] = item;
    });
  }
  var ret = {eye: [], brow: [], nose: [], mouse: []};
  allItem.forEach(function (item) {
    var tmp = {
      url: config.rootUrl + item.rurl,
      starid: item.starid,
      otype: item.otype,
      partid: item.partid,
      order: item.order,
      desc: item.desc
    };
    if (allStar) {
      tmp.starUrl = config.rootUrl + allStar[item.starid].rpath;
      tmp.starName = allStar[item.starid].name;
    }
    if (item.otype == EnumParts.brow) ret.brow.push(tmp);
    else if (item.otype == EnumParts.eye) ret.eye.push(tmp);
    else if (item.otype == EnumParts.nose) ret.nose.push(tmp);
    else if (item.otype == EnumParts.mouse) ret.mouse.push(tmp);
  });

  this.body = appUtil.fout(ret);
});
